package com.qcby.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qcby.admin.form.RolePageForm;
import com.qcby.admin.model.SysRole;
import com.qcby.admin.dao.SysRoleMapper;
import com.qcby.admin.service.SysRoleMenuService;
import com.qcby.admin.service.SysRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qcby.admin.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 角色信息表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2024-09-01
 */
@Service("roleService")
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {
    @Autowired
    private SysRoleMapper roleMapper;
    @Autowired
    private SysRoleMenuService roleMenuService;

    @Override
    public List<String> selectRoleKeyByUserId(Long id) {
        //判断是否是管理员 如果是返回集合中只需要有admin
        if(id == 1L){
            List<String> roleKeys = new ArrayList<>();
            roleKeys.add("admin");
            return roleKeys;
        }
//否则查询用户所具有的角色信息
        return getBaseMapper().selectRoleKeyByUserId(id);
    }
    @Override
    public RoleVo roleList(RolePageForm roleForm) {
        //初始化分页查询
        Page<SysRole> page = new Page<>(roleForm.getPageNum(), roleForm.getPageSize());
        //构建查询条件
        LambdaQueryWrapper<SysRole> lqw=new LambdaQueryWrapper<>();
        //根据角色名称进行模糊查询
        lqw.like(StringUtils.hasText(roleForm.getRoleName()),SysRole::getRoleName,roleForm.getRoleName());
        //根据角色状态进行查询
        lqw.eq(StringUtils.hasText(roleForm.getStatus()), SysRole::getStatus,roleForm.getStatus());
        //按照role_sort进行升序排序
        lqw.orderByAsc(SysRole::getRoleSort);
        //执行分页查询
        page = roleMapper.selectPage(page,lqw);
        //封装分页结果,转换成VO
        RoleVo rolevo=new RoleVo();
        rolevo.setRows(page.getRecords());
        rolevo.setTotal(page.getTotal());
        //System.out.println(page.getTotal());
        return rolevo;
    }
}
